/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package monitor;

import DATABASE.ConnectionManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author DS47
 */
public class MonitorRepositorio extends MonitorErro{
    /**
     *
     * @param cadastro
     * @throws MonitorErro
     */
    public void cadastrarMonitor(Monitor cadastro) throws MonitorErro {
        if (cadastro != null) {
            String comandoSQL     =null;//TEMP null 
            //comandoSQL = "insert into monitor (Nome,Sexo, Endereco, Email, Telefone, Rg, Dn,Funcao) values ('" + cadastro.get() + "','" + cadastro.getSexo() + "','" + cadastro.getEndereco() + "','" + cadastro.getEmail() + "','" + cadastro.getTelefone() + "','" + cadastro.getRg() + "','" + cadastro.getDn() + "','" + cadastro.getFuncao() + "')";
            System.out.println(comandoSQL);
            try {
                Statement stmt = ConnectionManager.getStatement();
                stmt.executeUpdate(comandoSQL);
                stmt.close();
            } catch (SQLException e) {
                System.out.println("Eh aqui o problema!!!");
                e.printStackTrace();
            }
        } else {
            throw new MonitorErro();
        }
    }

    /**
     *
     * @return @throws MonitorErro
     */
    public List listarMonitor() throws MonitorErro {

        ResultSet rs;
        List lista = new ArrayList();
        String comandoSQL;
        comandoSQL = "select * from monitor";
        System.out.println(comandoSQL);
        try {
            Statement stmt = ConnectionManager.getStatement();
            rs = stmt.executeQuery(comandoSQL);
            while (rs.next()) {
                //#############################
                Monitor f = new Monitor(); //TEMP!!!! DEPOIS TEM QUE REMOVERR!!!
               
                // Monitor f = new Monitor(rs.getInt("IdMonitor"), rs.getString("Nome"), rs.getString("Sexo"), rs.getString("Telefone"), rs.getString("Endereco"), rs.getString("Email"), rs.getString("Rg"), rs.getString("Dn"), rs.getString("Funcao"));
                System.out.println(rs.getString("Nome"));
                lista.add(f);
            }
            stmt.close();
        } catch (SQLException e) {
            System.out.println("� aqui o problema!!!");
            e.printStackTrace();
        }
        return lista;

    }

    /**
     *
     * @param modifica
     * @throws MonitorErro
     */
    public void editarMonitor(Monitor modifica) throws MonitorErro {
        if (modifica != null) {
            String comandoSQL        =null;
            //comandoSQL = "update monitor set Nome = '" + modifica.getNome() + "', Sexo = '" + modifica.getSexo() + "' Endereco = '" + modifica.getEndereco() + "' Email = '" + modifica.getEmail() + "' Telefone = '" + modifica.getTelefone() + "' Rg = '" + modifica.getRg() + "' Dn = '" + modifica.getDn() + "'Funcao='" + modifica.getFuncao();

            System.out.println(comandoSQL);
            try {
                Statement stmt = ConnectionManager.getStatement();
                stmt.executeUpdate(comandoSQL);
                stmt.close();
            } catch (SQLException e) {
                System.out.println("é aqui o problema!!!");
                e.printStackTrace();
            }
        } else {
            throw new MonitorErro();
        }
    }

    /**
     *
     * @param exclui
     * @throws MonitorErro
     */
    public void excluirMonitor(Monitor exclui) throws MonitorErro {
        if (exclui != null) {
            String comandoSQL       =null;
            //comandoSQL = "delete from monitor where IdMonitor = " + exclui.getId();
            System.out.println(comandoSQL);
            try {
                Statement stmt = ConnectionManager.getStatement();
                stmt.executeUpdate(comandoSQL);
                stmt.close();
            } catch (SQLException e) {
                System.out.println("� aqui o problema!!!");
                e.printStackTrace();
            }
        } else {
            throw new MonitorErro();
        }
    }

    /**
     *
     * @param nome
     * @throws MonitorErro
     */
    public void pesquisarMonitor(String nome) throws MonitorErro {

        ResultSet rs;
        String comandoSQL;
        comandoSQL = "select * from monitor where Nome like " + nome + "%";
        System.out.println(comandoSQL);
        try {
            Statement stmt = ConnectionManager.getStatement();
            stmt.executeUpdate(comandoSQL);
            stmt.close();
        } catch (SQLException e) {
            try {
                System.out.println("� aqui o problema!!!");
                e.printStackTrace();
                throw new MonitorErro();
            } catch (MonitorErro ex) {
                Logger.getLogger(MonitorRepositorio.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }

    public Monitor pesquisarMonitor(int id) throws MonitorErro {
        ResultSet rs;
        Monitor monitor = new Monitor();
        String comandoSQL;
        comandoSQL = "select * from monitor where IdMonitor=" + id;
        System.out.println(comandoSQL);
        try {
            Statement stmt = ConnectionManager.getStatement();
            rs = stmt.executeQuery(comandoSQL);
            while (rs.next()) {
                monitor.setId(rs.getInt("IdMonitor"));
                /* monitor.setNome(rs.getString("Nome"));
                 monitor.setSexo(rs.getString("Sexo"));
                 monitor.setTelefone(rs.getString("Telefone"));
                 monitor.setEndereco(rs.getString("Endereco"));
                 monitor.setRg(rs.getString("Rg"));
                 monitor.setDn(rs.getString("Dn"));
                 monitor.setEmail(rs.getString("Email"));
                 monitor.setFuncao(rs.getString("Funcao"));


                 System.out.println(rs.getString("Nome"));*/
            }
            stmt.close();
        } catch (SQLException e) {
            System.out.println("é aqui o problema!!!");
            e.printStackTrace();
        }
        return monitor;
    }

   
}
